package cn.jx.asc.service.impl;

import cn.jx.asc.dao.mapper.RoleMapper;
import cn.jx.asc.dao.mapper.UserMapper;
import cn.jx.asc.pojo.entity.User;
import cn.jx.asc.pojo.vo.ProfileInfoVO;
import cn.jx.asc.pojo.vo.RoleVO;
import cn.jx.asc.service.ProfileService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

/**
 * @author lhg
 */

@Service
public class ProfileServiceImpl implements ProfileService {
    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private UserMapper userMapper;
    @Override
    public RoleVO queryRoleByUserId(String userId){
        return roleMapper.queryRoleByUserId(userId);
    }

    @Transactional(propagation = Propagation.REQUIRED,rollbackFor=Exception.class)
    @Override
    public boolean modifyBasicInfo(User user) {

        final int i = userMapper.updateById(user);
        if ( i != 1 ){
            return false;
        }
        return true;
    }

    @Transactional(propagation = Propagation.REQUIRED,rollbackFor=Exception.class)
    @Override
    public Boolean modifyPassword(User user) {
        int i = userMapper.updateById(user);
        return i ==1;
    }

    @Transactional(propagation = Propagation.REQUIRED,rollbackFor=Exception.class)
    @Override
    public Boolean modifyAvatar(User user) {
        int i = userMapper.updateById(user);
        return  i ==1;
    }

    static void setProfileInfoVO(ProfileInfoVO infoVO, User user) {
        infoVO.setId(user.getId());
        infoVO.setUsername(user.getUsername());
        infoVO.setRealname(user.getRealname());
        infoVO.setProfilePhoto(user.getProfilePhoto());
        infoVO.setAge(user.getAge());
        infoVO.setSex(user.getSex());
        infoVO.setEmail(user.getEmail());
        infoVO.setTelephone(user.getTelephone());
    }

}
